package com.beta.service.pay;

import com.alibaba.fastjson.JSON;
import com.beta.dto.pay.PayRequestDto;
import com.beta.em.pay.PayRoutes;
import com.beta.pay.yeepay.core.Yeepay;
import com.beta.pay.yeepay.enums.PayProductType;
import com.beta.pay.yeepay.request.PayRequest;
import com.beta.pay.yeepay.response.PayResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * Created by vector on 2016/11/25.
 */
@Service
public class YeepayStrategy implements PayStrategy {

    @Value("${yeepay.pay.fee}")
    private String yeepayFee = "0.0045";

    @Value("${wepay.pay.fee}")
    private String wepayFee = "0.007";

    @Value("${alipay.pay.fee}")
    private String alipayFee = "0.007";

    @Value("${order.expire.minutes}")
    private Long orderExpireMinutes;

    @Autowired
    private Yeepay yeepay;

    @Override
    public String generatePayParams(PayRoutes payRoutes, PayRequestDto payRequestPDto) {
        PayRequest payRequest = new PayRequest(payRequestPDto.getPaymentCode());
        payRequest.setAmount(payRequestPDto.getAmount());
        payRequest.setProductName(payRequestPDto.getProductName());
        payRequest.setProductDesc(payRequestPDto.getProductDesc());
        payRequest.setCallbackUrl(payRequestPDto.getCallbackUrl());
        payRequest.setInvalidMinutes(payRequestPDto.getInvalidMinutes());
        payRequest.setUserNo(payRequestPDto.getUserCode());
        payRequest.setPayProductType(getPayProductType(payRoutes));
        payRequest.setClientIp(payRequestPDto.getClientIp());
        PayResponse payResponse = yeepay.pays().pay(payRequest);
        return JSON.toJSONString(payResponse);
    }

    private PayProductType getPayProductType(PayRoutes payRoutes) {
        switch (payRoutes) {
            case YEEPAY:
                return PayProductType.ONEKEY;
            case WEPAY:
                return PayProductType.WECHATAPP;
            default:
                return null;
        }
    }

    @Override
    public PayRoutes[] getPayRoutes() {
        return new PayRoutes[]{
                PayRoutes.YEEPAY,
                PayRoutes.WEPAY,
                PayRoutes.ALIPAY
        };
    }

    @Override
    public Long getPayExpireMinutes(PayRoutes payRoutes) {
        return orderExpireMinutes;
    }

    @Override
    public BigDecimal getPayFeeRate(PayRoutes payRoutes) {
        switch (payRoutes) {
            case YEEPAY:
                return new BigDecimal(yeepayFee);
            case WEPAY:
                return new BigDecimal(wepayFee);
            case ALIPAY:
                return new BigDecimal(alipayFee);
            default:
                return new BigDecimal(yeepayFee);
        }
    }

}
